const axios = require("axios");
const WinexAuthorizationGenerator = require("../WinexAuthorizationGenerator");

class WinexInpatientBaseGenerator {

    constructor(redisHost = "127.0.0.1", redisPort = 6379, redisDb = 0) {
        this.redisHost = redisHost;
        this.redisPort = redisPort;
        this.redisDb = redisDb;
        this.authorization = null;
    }

    /**
     * 
     * @param {{wardId: string, wardNumber: string, wardName: string}} ward 
     * @returns {Promise<{wardId: string, wardNumber: string, wardName: string, roomId: string, roomNumber: string, roomName: string}[]>}
     */
    async getRooms(ward) {
        const winexAuthorizationGenerator = new WinexAuthorizationGenerator();
        this.authorization = await winexAuthorizationGenerator.getAuthorization("adminAuthorizations");
        const url = `${this.authorization.hostname}/mdm-nurse/api/v1/app_execute_mdm/inpatient/ward_and_room_tree/query/by_soid`;
        const data = JSON.stringify({
            "hospitalSoid": this.authorization.hospitalSOID,
            "serviceHospitalSOID": this.authorization.serviceHospitalSOID,
            "wardId": ward.wardId
        });
        const config = {
            method: "post",
            maxBodyLength: Infinity,
            url: url,
            headers: {
                "Content-Type": "application/json",
                "Authorization": this.authorization.authorization,
            },
            data: data,
        };
        if (config.url.startsWith("https://")) {
            config.httpsAgent = new (require("https").Agent)({ rejectUnauthorized: false });
        }
        const response = await axios.request(config);
        let rooms = response.data.data[0].wardList[0].roomList.map(room => {
            return {
                wardId: room.wardId,
                wardNumber: room.wardNo,
                wardName: room.wardName,
                roomId: room.inpatRoomId,
                roomNumber: room.inpatRoomNo,
                roomName: room.roomName
            }
        })
        return rooms;
    }

    /**
     * 
     * @param {{wardId: string, wardNumber: string, wardName: string}} ward 
     * @param {number} numberOfRooms 
     * @param {string} roomNamePrefix 
     * @returns {Promise<{wardId: string, wardNumber: string, wardName: string, roomId: string, roomNumber: string, roomName: string}[]>}
     */
    async create(ward, numberOfRooms, roomNamePrefix = "房间") {
        const winexAuthorizationGenerator = new WinexAuthorizationGenerator();
        this.authorization = await winexAuthorizationGenerator.getAuthorization("adminAuthorizations");
        let url = `${this.authorization.hostname}/mdm-nurse/api/v1/app_execute_mdm/inpatient/inpatient_room/save`;
        const rooms = [];
        for (let i = 0; i < numberOfRooms; i++) {
            rooms.push({
                "wardId": ward.wardId,
                "inpatRoomNo": `ROOM-${String(i + 1).padStart(3, "0")}-${String(Date.now()).slice(-5)}`,
                "roomName": `${roomNamePrefix}${String(i + 1).padStart(3, "0")}`,
                "roomTypeCode": "959832",
                "roomGradeCode": "959839",
                "roomClassCode": "399669591",
                "encChargeServiceId": ""
            });
        }
        const data = JSON.stringify(rooms);
        const config = {
            method: "post",
            maxBodyLength: Infinity,
            url: url,
            headers: {
                "Content-Type": "application/json",
                "Authorization": this.authorization.authorization,
            },
            data: data,
        };
        if (config.url.startsWith("https://")) {
            config.httpsAgent = new (require("https").Agent)({ rejectUnauthorized: false });
        }
        const response = await axios.request(config);
        if (response.data.success) {
            return await this.getRooms(ward);
        }
        return [];
    }

}

module.exports = WinexInpatientBaseGenerator;